$(document).ready(function () {
	var layerFrame;
	bootbox.setDefaults({locale: framework.locale});

	// 复选框
	$('.i-checks').iCheck({checkboxClass: 'icheckbox_square-green', radioClass: 'iradio_square-green',});
	// 选择所有
	$("input[name='checkAll']").on('ifChanged', function () {
		$("input[name='ids']").iCheck($(this).is(":checked") == true ? 'check' : 'uncheck');
	});

	//跳转
	var $listForm = $("#listForm");

	// 页码跳转
	$.pageSkip = function (pageNumber) {
		$("#pageNumber").val(pageNumber);
		if (typeof listTemplate == 'undefined') {
			$listForm.submit();
		} else {
			loadTemplate();
		}
		//return false;
	}

	// 删除单条记录
	$(document).off("click", ".btn-delete-loippi").on("click", ".btn-delete-loippi", function () {
		var $this = $(this);
		bootbox.confirm(message("admin.dialog.deleteConfirm"), function (result) {
			if (result) {
				$.ajax({
					url: "delete.jhtml",
					type: "POST",
					data: {ids: $this.attr('data-id')},
					dataType: "json",
					cache: false,
					success: function (message) {
						window.setTimeout(function () {
							if (typeof listTemplate == 'undefined') {
								$listForm.submit();
							} else {
								loadTemplate();
							}
						}, 1000);
					}
				});
			}
		});
	});

	// 删除多条记录
	$(".btn-delete-loippi-group").click(function () {
		var $checkeds = jQuery($("input[name='ids']:checked"));

		if ($checkeds.length == 0) {
			art.warn('请选择删除记录！');
			return;
		}

		bootbox.confirm(message("admin.dialog.deleteConfirm"), function (result) {
			if (result) {
				$.ajax({
					url: "delete.jhtml",
					type: "POST",
					data: $checkeds.serialize(),
					dataType: "json",
					cache: false,
					success: function (message) {
						art.message(message);
						if (message.type == "success") {
							window.setTimeout(function () {
								if (typeof listTemplate == 'undefined') {
									$listForm.submit();
								} else {
									loadTemplate();
								}
							}, 1000);
						}
					}
				});
			}
		});
	});

});

function initCheck(){
	// 复选框
	$('.i-checks').iCheck({checkboxClass: 'icheckbox_square-green', radioClass: 'iradio_square-green',});
	// 选择所有
	$("input[name='checkAll']").on('ifChanged', function () {
		$("input[name='ids']").iCheck($(this).is(":checked") == true ? 'check' : 'uncheck');
	});
}

function loadTemplate() {
	var index = layer.load(1, {
		shade: [0.1, '#fff'] //0.1透明度的白色背景
	});
	var data = $("#listForm").serialize();
	$.get(listTemplate, data, function (result, status) {
		$("#listForm .dataTables_wrapper").empty();
		$("#listForm .dataTables_wrapper").html(result);
		layer.close(index);
		initCheck();
	}, "html");

	//$.ajax({
	//    cache: false,
	//    type: "GET",
	//    url: listTemplate,
	//    dataType:"html",
	//    data: $("#listForm").serialize(),// 你的formid
	//    async: true,
	//    error: function (request) {
	//        alert("请求失败");
	//        layer.close(index);
	//    },
	//    success: function (data) {
	//        $("#listForm .dataTables_wrapper").empty();
	//        $("#listForm .dataTables_wrapper").html(data);
	//        layer.close(index);
	//    }
	//});
}

function showRight(){
	var rightPanel = $("#right-panel");
	var morePanel = $("#more-panel");
	if(rightPanel.is(":hidden")){
		rightPanel.show();
		morePanel.empty();
	}
}

function showMore(htmlObj){
	var rightPanel = $("#right-panel");
	var morePanel = $("#more-panel");
	if(!rightPanel.is(":hidden")){
		rightPanel.hide();
	}
	morePanel.empty();
	morePanel.append(htmlObj);
}